
package it.csttech.date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;

/**
 * OVERVIEW This class is a resource . It is called through a form and the use
 * of a cookie
 * 
 * @author marcello.felappi
 * @author marco.dalena
 */
@Path("/form")
public class FormComplexObject {

	@POST
	@Path("/test")
	public Response addUser(
		@FormParam("name") String name, @FormParam("float") Float number,
		@FormParam("date") Date date,
		@CookieParam(value = "date") Date generationDate,
		@Context HttpServletRequest request) {

		ParamComplexObject pcomplex=new ParamComplexObject(date,name,number);
		if (generationDate == null) {
			Locale locale = request.getLocale();
			Date cookieDate = new Date();
			DateFormat format =
				SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT, locale);
			return Response.status(200).entity(
				pcomplex).type(MediaType.APPLICATION_JSON).cookie(
				new NewCookie("date", format.format(cookieDate))).build();
		}
		else {
			System.out.println(generationDate);
			return Response.status(200).entity(
				pcomplex).type(MediaType.APPLICATION_JSON).build();
		}
	}
}
